package com.wqtz.chechai.view.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseFragment;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.DataCleanManager;
import com.wqtz.chechai.utils.SPUtils;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.view.activities.ChangeUserPhoneActivity;
import com.wqtz.chechai.view.activities.GestureSignActivity;
import com.wqtz.chechai.view.activities.LoginActivity;
import com.wqtz.chechai.view.activities.MainActivity;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hugeterry.updatefun.UpdateFunGO;
import cn.hugeterry.updatefun.config.UpdateKey;
import me.nereo.multi_image_selector.bean.Image;

import static com.wqtz.chechai.widget.GestureView.STATE_REGISTER;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 13:36
 */

public class MoreFragment extends BaseFragment {
	@BindView(R.id.tv_title)
	TextView mTvLoginTitle;
	@BindView(R.id.iv_head)
	ImageView mIvHead;
	@BindView(R.id.tv_name)
	TextView mTvName;
	@BindView(R.id.tv_com)
	TextView mTvCom;
	@BindView(R.id.tv_phone)
	TextView mTvPhone;
	@BindView(R.id.tv_phone_more)
	TextView mTvPhoneMore;
	@BindView(R.id.ll_phone)
	LinearLayout mLlPhone;
	@BindView(R.id.tv_gesture)
	TextView mTvGesture;
	@BindView(R.id.tv_haGesture)
	TextView mTvHaGesture;
	@BindView(R.id.ll_gesture)
	LinearLayout mLlGesture;
	@BindView(R.id.tv_suggestion)
	TextView mTvSuggestion;
	@BindView(R.id.tv_clearCache)
	TextView mTvClearCache;
	@BindView(R.id.tv_cacheSize)
	TextView mTvCacheSize;
	@BindView(R.id.ll_clear)
	LinearLayout mLlClear;
	@BindView(R.id.tv_update)
	TextView mTvUpdate;
	@BindView(R.id.tv_version)
	TextView mTvVersion;
	@BindView(R.id.ll_update)
	LinearLayout mLlUpdate;
	@BindView(R.id.tv_about)
	TextView mTvAbout;
	@BindView(R.id.btn_login_out)
	Button mBtnLoginOut;
	private String userName;
	private long imageSize;
	private long caCheSize;
	private String cache;
	private static String appVersionName;
	private String idImagePath;
	private File idimages;

	@Override
	public BasePresent createPresenter() {
		return null;
	}

	@Override
	protected int getContentView() {
		return R.layout.fragment_more;
	}

	@Override
	protected void init(View view) {
		ButterKnife.bind(this, view);
		//此处填上在http://fir.im/注册账号后获得的API_TOKEN以及APP的应用ID
		UpdateKey.API_TOKEN = "0af58cd0b8153e28d6316d1ed69cc490";
		UpdateKey.APP_ID = "5853b998959d69348e000900";
		//如果你想通过Dialog来进行下载，可以如下设置
		//UpdateKey.DialogOrNotification=UpdateKey.WITH_DIALOG;
		UpdateFunGO.init(getContext());
		getCacheSize();
	}

	@Override
	protected void loadDatas() {
		userName = MainActivity.CURRENT_USER.getUsername();
		if (!StringUtils.isEmpty(userName)) {
			mTvPhoneMore.setText(StringUtils.getSecPhoneNo(userName));
		}
		if (!StringUtils.isEmpty(userName)) {
			mTvName.setText(userName);
		}
		mTvVersion.setText(getAPPVersionCode(getContext()));

	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO: inflate a fragment view
		View rootView = super.onCreateView(inflater, container, savedInstanceState);
		ButterKnife.bind(this, rootView);
		return rootView;
	}

	@OnClick({R.id.btn_login_out, R.id.ll_update, R.id.ll_phone,R.id.ll_gesture,R.id.ll_clear})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_login_out: {
				AVUser.getCurrentUser().logOut(); //注销用户
				if (SPUtils.saveState(getContext(), STATE_REGISTER)) {
					Intent intent = new Intent(getContext(), LoginActivity.class);
					startActivity(intent);
					getActivity().finish();
				} else {
					ToastUtils.show(getContext(), "手势密码无法注销", 2000);
				}

			}
			break;
			case R.id.ll_update: {
				UpdateFunGO.manualStart(getContext());
			}
			break;
			case R.id.ll_gesture: {
				Intent intent = new Intent(getContext(), GestureSignActivity.class);
				startActivity(intent);
			}
			break;
			case R.id.ll_phone:{
				Intent intent = new Intent(getContext(), ChangeUserPhoneActivity.class);
				startActivity(intent);
			}break;
			case R.id.ll_clear:{
				DataCleanManager.cleanApplicationData(getContext());
				DataCleanManager.deleteFolderFile(idImagePath,false);
				getCacheSize();
			}break;
		}

	}

	@Override
	public void onResume() {
		super.onResume();
		getCacheSize();
		if (SPUtils.getGestureCode(getContext()) == STATE_REGISTER) {
			mTvHaGesture.setText(R.string.none_gesture);
		} else {
			mTvHaGesture.setText(R.string.has_gesture);
		}
	}

	public void getCacheSize() {
		// 驾照保存地址
		 idImagePath = Environment.getExternalStorageDirectory()+"/WImage";
		 idimages = new File(Environment.getExternalStorageDirectory(),"WImage");
		try {
			imageSize = DataCleanManager.getFolderSize(idimages);
			caCheSize = DataCleanManager.getFolderSize(new File("/data/data/com.wqtz.chechai/cache"));
			cache = (imageSize+caCheSize)/1024/1024+"MB";
			if(imageSize+caCheSize==0){
				return;
			}
			mTvCacheSize.setText(cache);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取apk的版本号 currentVersionCode
	 *
	 * @param ctx
	 * @return
	 */
	public static String getAPPVersionCode(Context ctx) {
		int currentVersionCode = 0;
		PackageManager manager = ctx.getPackageManager();
		try {
			PackageInfo info = manager.getPackageInfo(ctx.getPackageName(), 0);
			appVersionName = info.versionName; // 版本名
			currentVersionCode = info.versionCode; // 版本号
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return appVersionName+currentVersionCode;
	}


}
